package com.mingsoft.led;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mingsoft.led.util.Configure;
import com.mingsoft.led.util.Utils;

public class FileBean {
	private String rootPath = null;
	private Map<Integer, File> videoFiles = new HashMap<Integer, File>();
	private Map<Integer, File> pptFiles = new HashMap<Integer, File>();
	private Map<Integer, File> pictureFiles = new HashMap<Integer, File>();
	private List<FileName> videoFileNames = new ArrayList<FileName>();
	private List<FileName> pptFileNames = new ArrayList<FileName>();
	private List<FileName> pictureFileNames = new ArrayList<FileName>();
	private String timestmp = null;

	private FileBean() {
		rootPath = Configure.getInstance().get(Configure.KEY_ROOT_PATH);
		System.out.println(rootPath);
	}

	private static FileBean fileBean = new FileBean();

	public static FileBean getInstance() {
		return fileBean;
	}

	public void update() {
		if (timestmp == null) {
			synchronized (this) {
				if (timestmp == null) {

					this.setTimestmp("" + System.currentTimeMillis());

					System.out.println("Start to read resource files in path:"
							+ rootPath);

					File pptPath = new File(rootPath + "/ppt/");
					int index = 0;
					System.out.println("Start to read ppt files in path:"
							+ pptPath);
					if (pptPath.exists()) {
						for (File f : pptPath.listFiles()) {
							if (f.getName().toLowerCase().endsWith(".ppt")) {
								index++;
								String id = f.getName().replace(".ppt", "");
								id = Utils.toUTF8String(id);
								pptFiles.put(index, f);
								FileName fileName = new FileName();
								fileName.setFileName(id);
								fileName.setIndex(index);
								pptFileNames.add(fileName);
								System.out.println(fileName.getFileName());
							}
						}
					}
					File videoPath = new File(rootPath + "/video/");
					index = 0;
					System.out.println("Start to read video files in path:"
							+ videoPath);
					if (videoPath.exists()) {
						for (File f : videoPath.listFiles()) {
							if (f.getName().toLowerCase().endsWith(".wmv")
									|| f.getName().toLowerCase()
											.endsWith(".mpg")) {
								index++;
								String id = f.getName().replace(".wmv", "")
										.replace(".mpg", "");
								id = Utils.toUTF8String(id);
								videoFiles.put(index, f);
								FileName fileName = new FileName();
								fileName.setFileName(id);
								fileName.setIndex(index);
								videoFileNames.add(fileName);
								System.out.println(fileName.getFileName());
							}
						}
					}
					File picturePath = new File(rootPath + "/picture/");
					index = 0;
					System.out.println("Start to read picture files in path:"
							+ picturePath);
					if (picturePath.exists()) {
						for (File f : picturePath.listFiles()) {
							if (f.isDirectory()) {
								index++;
								String id = f.getName();
								id = Utils.toUTF8String(id);
								pictureFiles.put(index, f);
								FileName fileName = new FileName();
								fileName.setFileName(id);
								fileName.setIndex(index);
								pictureFileNames.add(fileName);
								System.out.println(fileName.getFileName());
							}
						}
					}
				}
			}
		}
	}

	public String getRootPath() {
		return rootPath;
	}

	public void setRootPath(String rootPath) {
		this.rootPath = rootPath;
	}

	public Map<Integer, File> getPptFiles() {
		return pptFiles;
	}

	public void setPptFiles(Map<Integer, File> pptFiles) {
		this.pptFiles = pptFiles;
	}

	public Map<Integer, File> getVideoFiles() {
		return videoFiles;
	}

	public void setVideoFiles(Map<Integer, File> videoFiles) {
		this.videoFiles = videoFiles;
	}

	public List<FileName> getPptFileNames() {
		return pptFileNames;
	}

	public void setPptFileNames(List<FileName> pptFileNames) {
		this.pptFileNames = pptFileNames;
	}

	public List<FileName> getVideoFileNames() {
		return videoFileNames;
	}

	public void setVideoFileNames(List<FileName> videoFileNames) {
		this.videoFileNames = videoFileNames;
	}

	public Map<Integer, File> getPictureFiles() {
		return pictureFiles;
	}

	public void setPictureFiles(Map<Integer, File> pictureFiles) {
		this.pictureFiles = pictureFiles;
	}

	public List<FileName> getPictureFileNames() {
		return pictureFileNames;
	}

	public void setPictureFileNames(List<FileName> pictureFileNames) {
		this.pictureFileNames = pictureFileNames;
	}

	public String getTimestmp() {
		return timestmp;
	}

	public void setTimestmp(String timestmp) {
		this.timestmp = timestmp;
	}

}
